#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys, os

# Base directory
base_dir = os.path.join(sys.prefix,'Games','MyGarden')

# Installation
if sys.argv[1] == "-install":
    # Create shortcut to batch file on desktop
    desktop = get_special_folder_path("CSIDL_COMMON_DESKTOPDIRECTORY")
    if not desktop:
        desktop = get_special_folder_path("CSIDL_DESKTOPDIRECTORY")
        
    file = os.path.join(desktop,'MyGarden.lnk')
    if not os.path.exists(file):
        link = create_shortcut(os.path.join(base_dir,'mygarden.bat'),
                               'Shortcut to MyGarden',
                               file,
                               '',
                               base_dir,
                               os.path.join(base_dir,'mygarden.ico'),
                               0)
        file_created(file)
        print "Shortcut created on the desktop"
    
    # Create shortcut to batch file in start menu
    start_menu = get_special_folder_path("CSIDL_COMMON_STARTMENU")
    if not start_menu:
        start_menu = get_special_folder_path("CSIDL_STARTMENU")
        
    # Create folder
    folder = os.path.join(start_menu,"MyGarden")
    if not os.path.exists(folder):
        os.mkdir(folder)
        directory_created(folder)
    
    # Shortcut to bat file
    file = os.path.join(folder,'MyGarden.lnk')
    if not os.path.isfile(file):
        link = create_shortcut(os.path.join(base_dir,'mygarden.bat'),
                               'Shortcut to MyGarden',
                               file,
                               '',
                               base_dir,
                               os.path.join(base_dir,'mygarden.ico'),
                               0)
        file_created(file)
    
    # Shortcut to plants folder
    file = os.path.join(folder,'MyGarden plants.lnk')
    if not os.path.isfile(file):
        link = create_shortcut(os.path.join(base_dir,'plants'),
                               'Shortcut to MyGarden plants folder',
                               file,
                               '',
                               base_dir,
                               os.path.join(base_dir,'mygarden.ico'),
                               0)
        file_created(file)
        
    # Shortcut to README file
    # Rename README to README.txt to make it easier for Windows users
    readme = os.path.join(base_dir,'doc','README.txt')
    os.rename(os.path.join(base_dir,'doc','README'),
              readme)
    file = os.path.join(folder,'README.lnk')
    if not os.path.isfile(file):
        link = create_shortcut(readme,
                               'Shortcut to MyGarden README file',
                               file,
                               '',
                               base_dir,
                               os.path.join(base_dir,'mygarden.ico'),
                               0)
        file_created(file)
        
    # Shortcut to manual
    file = os.path.join(folder,'MyGarden manual.lnk')
    if not os.path.isfile(file):
        link = create_shortcut(os.path.join(base_dir,'doc','MyGarden.pdf'),
                               'Shortcut to MyGarden manual',
                               file,
                               '',
                               base_dir,
                               os.path.join(base_dir,'mygarden.ico'),
                               0)
        file_created(file)
        
    print "Shortcuts created in the start menu"
        
    # Change file extension to .pyw for main.py
    main_pyw = os.path.join(base_dir,"game","game","main.pyw")
    os.rename(os.path.join(base_dir,"game","game","main.py"),
              main_pyw)
    # Set as created, so it's automatically removed at uninstall
    file_created(main_pyw)
    
    print "Installation finished successfully!"

# Removal
elif sys.argv[1] == "-remove":
    # Remove Games/MyGarden folder
    os.remove(base_dir)
    # Remove Highscores
    os.remove(os.path.join(get_special_folder_path("CSIDL_APPDATA"),"MyGarden"))